<template>
  <div class="goods">
    <div class="wrap">
      <Crumb></Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: index == options1Active }"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Fn(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: index == options2Active }"
            v-for="(item, index) in options2"
            :key="index"
            @click="options2Fn(index, item.type)"
            >{{ item.txt }}</span
          >
          <!-- <span>实物礼品</span>
          <span>虚拟礼品</span> -->
        </li>
      </ul>
      <List :arr="arrshow" />
      <span class="Limgbox">
        <img src="../assets/img/26073947_j9gz.gif" width="100" alt="">
        {{isReachBottom?"加载完成":"加载中..."}}
      </span>
    </div>
  </div>
</template>                            
<script>
import { productsAPI } from "@/request/api";
import Crumb from "@/components/Crumb";
import List from "@/components/home/List";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils/index";
export default {
  data() {
    return {
      did: 0,
      type: 0,
      min: 0,
      max: 0,
      keyword: "",
      arr: [],
      arrshow: [],
      options1: [
        { min: 0, max: 10000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      options2: [
        { type: 0, txt: "全部" },
        { type: 1, txt: "实体商品" },
        { type: 2, txt: "虚拟商品" },
      ],
      options1Active: 0,
      options2Active: 0,
      page: 1,
      size: 8,
      isLogin: false,
      isReachBottom: true,
    };
  },
  components: {
    Crumb,
    List,
  },
  created() {
    this.keyword = this.$route.query.keyword;
    this.goodsSearch();
  },
  mounted() {
    window.addEventListener("scroll", this.scrollFn);
  },
  beforeDestroy() {
    window.addEventListener("scroll", this.scrollFn);
  },
  methods: {
    async goodsSearch() {
      let res = await productsAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      this.arr = res.data.data;
      this.arrshow = this.arr.filter((item, index) => index < 8);
      this.page = 1;
      this.isReachBottom = false;
       if(this.arrshow.length==this.arr.length){
        this.isReachBottom = true;
      }
    },
    options1Fn(idx, min, max) {
      this.options1Active = idx;
      // 更新查询条件
      (this.min = min), (this.max = max);
      this.goodsSearch();
    },
    options2Fn(idx, type) {
      this.options2Active = idx;
      // 更新查询条件
      this.type = type;
      this.goodsSearch();
    },
    scrollFn() {
      getScrollTop(), getClientHeight(), getScrollHeight();
      if(this.arrshow.length==this.arr.length){
        this.isReachBottom = true;
      }
      if (getClientHeight() + getScrollTop() >= getScrollHeight() - 400) {
        if (this.isLogin == false) {
          this.isLogin = true;
          
          setTimeout(()=>{
            this.page++;
            for (
            let i = (this.page - 1) * this.size;
            i < this.page * this.size;
            i++
          ) {
            if (i >= this.arr.length) {
                 this.isLogin = false;
                 break;
            } else {
              this.arrshow.push(this.arr[i]);
            }
          }
          this.isLogin = false;
          },5000)
          
        }
      }
    },
  },
  watch: {
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        this.keyword = newVal;
        this.goodsSearch();
      },
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";
@base-color: #0a328e;
.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
  .Limgbox{
    img{
 margin: 10px auto;
    }
   
  }
}
</style>